import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int lengthOfLongestSubstring(String str) {
        int len = str.length();
        if (len <= 0) {
            return 0;
        }
        char[] ch = str.toCharArray();

        int left = 0, right = 0;
        Queue<Character> queue = new LinkedList<>();
        queue.offer(ch[right++]);
        int ret = 1;
        while (left < right && right < len) {
            if (!queue.contains(ch[right])) {
                queue.offer(ch[right++]);
            } else {
                ret = Math.max(ret, right - left);
                while (queue.peek() != ch[right]) {
                    queue.poll();
                    left++;
                }
                queue.poll();
                left++;
                queue.offer(ch[right++]);
            }
        }

        ret = Math.max(ret, right - left);

        return ret;
    }
}